package com.nulldev.util.java.swing.extSwing.legacy;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.Stroke;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JPanel;

public class eGraph extends JPanel {

    int width = 200;
    int heigth = 200;
    int padding = 25;
    int labelPadding = 25;
    private Color lineColor = Color.yellow;//new Color(44, 102, 230, 180);
    private Color pointColor = new Color(100, 100, 100, 180);
    private Color gridColor = new Color(200, 200, 200, 200);
    private Color textColor = Color.WHITE;
    private static final Stroke GRAPH_STROKE = new BasicStroke(1f);
    int pointWidth = 4;
    int numberYDivisions = 10;
    private ArrayList<Double> scores;
    private boolean usePercent;
    public boolean disableBottomText;

    private double min = -1, max = -1;

    public eGraph(final ArrayList<Double> scores) {
        this.scores = scores;
        this.setBackground(new Color(0, 0, 0, 0));
    }

    public void resetMin() {
    	this.min = -1;
    }

    public void resetMax() {
    	this.max = -1;
    }

    public void setMin(final double min) {
    	this.min = min;
    }

    public void setMax(final double max) {
    	this.max = max;
    }

    public void setLineColor(final Color color) {
    	this.lineColor = color;
    }

    public void setGridColor(final Color color) {
    	this.gridColor = color;
    }

    public void setTextColor(final Color color) {
    	this.textColor = color;
    }

    public boolean usePercent() {
    	return this.usePercent;
    }

    public void usePercentages(final boolean value) {
    	this.usePercent = value;
    }
    
    @Override
    public void setForeground(final Color color) {
    	super.setForeground(color);
    	this.textColor = color;
    	this.lineColor = color;
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        double xScale = ((double) getWidth() - (2 * padding) - labelPadding) / (scores.size() - 1);
        double yScale = ((double) getHeight() - 2 * padding - labelPadding) / (getMaxScore() - getMinScore());

        List<Point> graphPoints = new ArrayList<Point>();
        for (int i = 0; i < scores.size(); i++) {
            int x1 = (int) (i * xScale + padding + labelPadding);
            int y1 = (int) ((getMaxScore() - scores.get(i)) * yScale + padding);
            graphPoints.add(new Point(x1, y1));
        }

        // draw white background
        g2.setColor(Color.WHITE);
        //g2.fillRect(padding + labelPadding, padding, getWidth() - (2 * padding) - labelPadding, getHeight() - 2 * padding - labelPadding);
        g2.setColor(Color.BLACK);

        // create hatch marks and grid lines for y axis.
        for (int i = 0; i < numberYDivisions + 1; i++) {
            int x0 = padding + labelPadding;
            int x1 = pointWidth + padding + labelPadding;
            int y0 = getHeight() - ((i * (getHeight() - padding * 2 - labelPadding)) / numberYDivisions + padding + labelPadding);
            int y1 = y0;
            if (scores.size() > 0) {
                g2.setColor(gridColor);
                g2.drawLine(padding + labelPadding + 1 + pointWidth, y0, getWidth() - padding, y1);
                g2.setColor(Color.BLACK);
                String yLabel = ((int) ((getMinScore() + (getMaxScore() - getMinScore()) * ((i * 1.0) / numberYDivisions)) * 100)) / 100.0 + (this.usePercent ? "%" : "");
                FontMetrics metrics = g2.getFontMetrics();
                int labelWidth = metrics.stringWidth(yLabel);
                g2.setColor(this.textColor);
                g2.drawString(yLabel, x0 - labelWidth - 5, y0 + (metrics.getHeight() / 2) - 3);
            }
            g2.drawLine(x0, y0, x1, y1);
        }

        // and for x axis
        if (!this.disableBottomText) {
            for (int i = 0; i < scores.size(); i++) {
                if (scores.size() > 1) {
                    int x0 = i * (getWidth() - padding * 2 - labelPadding) / (scores.size() - 1) + padding + labelPadding;
                    int x1 = x0;
                    int y0 = getHeight() - padding - labelPadding;
                    int y1 = y0 - pointWidth;
                    if ((i % ((int) ((scores.size() / 20.0)) + 1)) == 0) {
                        g2.setColor(gridColor);
                        g2.drawLine(x0, getHeight() - padding - labelPadding - 1 - pointWidth, x1, padding);
                        g2.setColor(Color.BLACK);
                        String xLabel = i + "";
                        FontMetrics metrics = g2.getFontMetrics();
                        int labelWidth = metrics.stringWidth(xLabel);
                        g2.setColor(this.textColor);
                        g2.drawString(xLabel, x0 - labelWidth / 2, y0 + metrics.getHeight() + 3);
                    }
                    g2.drawLine(x0, y0, x1, y1);
                }
            }
        }

        // create x and y axes
        g2.drawLine(padding + labelPadding, getHeight() - padding - labelPadding, padding + labelPadding, padding);
        g2.drawLine(padding + labelPadding, getHeight() - padding - labelPadding, getWidth() - padding, getHeight() - padding - labelPadding);

        Stroke oldStroke = g2.getStroke();
        g2.setColor(lineColor);
        g2.setStroke(GRAPH_STROKE);
        for (int i = 0; i < graphPoints.size() - 1; i++) {
            int x1 = graphPoints.get(i).x;
            int y1 = graphPoints.get(i).y;
            int x2 = graphPoints.get(i + 1).x;
            int y2 = graphPoints.get(i + 1).y;
            g2.drawLine(x1, y1, x2, y2);
        }

        g2.setStroke(oldStroke);
        g2.setColor(pointColor);
        for (int i = 0; i < graphPoints.size(); i++) {
            int x = graphPoints.get(i).x - pointWidth / 2;
            int y = graphPoints.get(i).y - pointWidth / 2;
            int ovalW = pointWidth;
            int ovalH = pointWidth;
            g2.fillOval(x, y, ovalW, ovalH);
        }
    }

//    @Override
//    public Dimension getPreferredSize() {
//        return new Dimension(width, heigth);
//    }

    private double getMinScore() {
    	if (this.min != -1) {
    		return this.min;
    	}
        double minScore = Double.MAX_VALUE;
        for (Double score : (ArrayList<Double>) scores.clone()) {
            try {
            	minScore = Math.min(minScore, score);
            }catch (Exception e) {
            	minScore = 500;
            }
        }
        return minScore;
    }

    private double getMaxScore() {
    	if (this.max != -1) {
    		return this.max;
    	}
        double maxScore = Double.MIN_VALUE;
        for (Double score : scores) {
            try {
            	maxScore = Math.max(maxScore, score);
            }catch (Exception e) {
            	maxScore = 1000;
            }
        }
        return maxScore;
    }

    public void add(final Double item) {
    	if (this.scores == null) {
    		this.scores = new ArrayList<Double>();
    	}
    	this.scores.add(item);
        invalidate();
        this.repaint();
    }
    
    public void addR(final Double item) {
    	if (this.scores == null) {
    		this.scores = new ArrayList<Double>();
    	}
    	this.scores.add(item);
        invalidate();
        this.repaint();
    }
    
    public void safeReset() {
    	if (this.scores.size() > 144) {
    		this.scores.remove(0);
    	}
    }

    public void setScores(final ArrayList<Double> scores) {
        this.scores = scores;
        invalidate();
        this.repaint();
    }

    public List<Double> getScores() {
        return scores;
    }
}
